# encoding = utf-8
import cv2
import sys
import os
sys.path.append(os.getcwd())
from config.env import Env
env = Env()

filepath = env.getImgPath() + "meinv1.jpg"
img = cv2.imread(filepath)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# openCv 人脸识别分类器
classifier = cv2.CascadeClassifier(env.getModelPath() + "haarcascade_frontalface_alt.xml")

# x = y = 10
# w = 100
color = (0, 255, 0)

faceRects = classifier.detectMultiScale(
    gray, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))
if len(faceRects):  # 大于0则检测到人脸
    for faceRect in faceRects:  # 单独框出每一张人脸
        x, y, w, h = faceRect
        # 框出人脸
        cv2.rectangle(img, (x, y), (x + h, y + w), color, 2)
        # 左眼
        cv2.circle(img, (x + w // 4, y + h // 4 + 30), min(w // 8, h // 8),
                   color)
        #右眼
        cv2.circle(img, (x + 3 * w // 4, y + h // 4 + 30), min(w // 8, h // 8),
                   color)
        #嘴巴
        cv2.rectangle(img, (x + 3 * w // 8, y + 3 * h // 4),
                      (x + 5 * w // 8, y + 7 * h // 8), color)
cv2.rectangle(img, (x, y), (x + w, y + w), color, 1)

cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()